package com.easysoft.framework.action;

import com.easysoft.commons.utils.FileUtils;
import com.easysoft.commons.utils.StringUtils;
import com.easysoft.commons.utils.WebConstants;
import com.easysoft.framework.annotation.logs.SystemControllerLog;
import com.easysoft.framework.beans.Page;
import com.easysoft.framework.enterprise.model.SysEnterprise;
import com.easysoft.framework.enterprise.model.SysEnterpriseView;
import com.easysoft.framework.enterprise.service.SysEnterpriseService;
import com.easysoft.framework.product.model.*;
import com.easysoft.framework.product.service.SysProductCategoryService;
import com.easysoft.framework.product.service.SysProductService;
import com.easysoft.framework.user.model.SysUser;
import com.easysoft.framework.user.model.SysUserView;
import com.easysoft.framework.user.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping(value = SystemProductAction.BASE_VIEW)
public class SystemProductAction {
    public static final String BASE_VIEW = "/product";
    private static final Logger logger = LoggerFactory.getLogger(SystemProductAction.class);

    @Autowired
    @Qualifier("service.SysProductService")
    private SysProductService productService;

    @Autowired
    @Qualifier("service.UserService")
    private UserService userService;

    @Autowired
    @Qualifier("service.SysEnterpriseService")
    private SysEnterpriseService enterpriseService;

    @Autowired
    @Qualifier("service.SysProductCategoryService")
    private SysProductCategoryService productCategoryService;

    @RequestMapping(value = "/listProduct", method = RequestMethod.GET)
    public ModelAndView listProduct(Page page, SysProductQuery query, HttpSession session) {
        logger.info("Call SystemProductAction.listProduct({},{})", page.toString(), query.toString());
        List<SysProductView> listProduct = productService.selectSysProductByPage(page, query);
        ModelAndView mv = new ModelAndView(BASE_VIEW + "/listProduct");
        mv.addObject("page", page);
        mv.addObject("query", query);
        mv.addObject("listProduct", listProduct);
        return mv;
    }

    @RequestMapping(value = "/editProduct", method = RequestMethod.GET)
    public ModelAndView editProduct(String action, @RequestParam(required = false) String id) {
        logger.info("Call SystemProductAction.editProduct({},{})", action, id);
        ModelAndView mv = new ModelAndView(BASE_VIEW + "/editProduct");
        List<SysUserView> listUser = userService.selectAllSysUser();
        List<SysEnterpriseView> listEnterprise = enterpriseService.selectAllSysEnterprise();
        List<SysProductCategoryView> listProductCategory = productCategoryService.selectSysProductCategory(false);
        mv.addObject("listUser",listUser);
        mv.addObject("listEnterprise",listEnterprise);
        mv.addObject("listProductCategory",listProductCategory);
        if (StringUtils.isNotNullAndBlank(action) && action.toLowerCase().equals("add")) {
            // 添加
            if (StringUtils.isNotNullAndBlank(id)) {
                return null;
            }
            return mv;
        }
        if (StringUtils.isNotNullAndBlank(action) && action.toLowerCase().equals("update")) {
            // 修改
            if (StringUtils.isNullOrBlank(id)) {
                return null;
            }
            SysProduct entity = productService.selectSysProductById(id);
            if (entity == null) {
                return null;
            }
            mv.addObject("entity", entity);
            return mv;
        }
        return null;
    }

    @RequestMapping(value = "/saveProduct", method = RequestMethod.POST)
    public int saveProduct(String action, SysProduct product, @RequestParam(value = "multipartFile", required = false) MultipartFile multipartFile, HttpServletRequest request, HttpSession session) {
        logger.info("Call SystemProductAction.saveProduct({},{})", action, product.toString());
        if(multipartFile != null && !("").equals(multipartFile)){
            product.setImages( FileUtils.savePicture(multipartFile,"product",request));
        }
        if (StringUtils.isNotNullAndBlank(action) && action.toLowerCase().equals("add")) {
            // 添加
            if (StringUtils.isNotNullAndBlank(product.getId())) {
                return 0;
            }
            return addSysProduct(product, session);
        }
        if (StringUtils.isNotNullAndBlank(action) && action.toLowerCase().equals("update")) {
            // 修改
            if (StringUtils.isNullOrBlank(product.getId())) {
                return 0;
            }
            return updateSysProduct(product, session);
        }
        return 0;
    }

    @SystemControllerLog(description = "新增商品信息")
    private int addSysProduct(SysProduct product, HttpSession session) {
        logger.info("Call SystemProductAction.addSysProduct({})", product.toString());
        product.setOperareor((String) session.getAttribute(WebConstants.SESSION_USER_ID));
        return productService.addSysProduct(product);
    }

    @SystemControllerLog(description = "修改商品信息")
    private int updateSysProduct(SysProduct product, HttpSession session) {
        logger.info("Call SystemProductAction.updateSysProduct({})", product.toString());
        product.setOperareor((String) session.getAttribute(WebConstants.SESSION_USER_ID));
        return productService.updateSysProduct(product);
    }

    @RequestMapping(value = "/deleteProduct", method = RequestMethod.POST)
    @SystemControllerLog(description = "删除商品信息")
    public int deleteSysProduct(String id) {
        logger.info("Call SystemProductAction.deleteSysTrademark({})", id);
        return productService.deleteSysProduct(id);
    }
}
